/*====================================================================================
                                                                     
		          O-Ring Production Networks
			        By Demir-Fieler-Xu-Yang
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
/Table 3
****************************************/

use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

reghdfe  Chgavgwage_adj_s unadjexshock ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

noi esttab col1, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


foreach y of varlist avgwage_adj_s supavgwage_w buyeravgwage_w domsales expintensity {
reghdfe  Chg`y' adjexshock ,absorb(sr) vce(cluster s)
estimates store `y'
estadd local "FE" "sr" , replace
}

noi esttab avgwage_adj_s supavgwage_w buyeravgwage_w domsales expintensity, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/*********************************************
/Table 4, Table A14, Table A15, and Table A16
**********************************************/
// Input composition //

//Worker composition

use "CALISAN_MIKRO_2012_2015",clear

replace sırano_donem=subinstr(sırano_donem,"_"," ",.)
split sırano_donem 
rename sırano_donem1 ID
rename donem_yıl year
rename donem_ay month
rename kazanc earnings
rename prım_gun_sayısı numofpremiumdays
rename fkyas age
rename cinsiyetaciklama gender
rename calisansgk employee_ID

destring ID, replace
replace earnings=subinstr(earnings,",",".",.)
destring earnings, replace

for X in any tanım : replace tanım=subinstr(tanım,"_"," ",.)
rename tanım1 occupation_code
destring occupation_code, replace

drop if numofpremiumdays<28 // Keep full-time workers
drop if numofpremiumdays>31
drop if gender==0

gen period=0 if  year==2011 | year==2012
replace period=1 if  year==2014 | year==2015
drop if period==.

collapse (mean) earnings ,by( ID  employee_ID period )

save temp3.dta,replace

*Worker wages at t=0
use temp3.dta,clear

collapse (mean) earnings,by(employee_ID period)
rename earnings earnings_lag

reshape wide earnings_lag,i(  employee_ID) j(period)

sort employee_ID 
save temp4.dta,replace

****
use temp3.dta,clear
reshape wide earnings,i(ID  employee_ID) j(period)

gen newemployee=(earnings1!=. & earnings0==.)
gen formeremployee=(earnings1==. & earnings0!=.)
gen contemployee=(earnings1!=. & earnings0!=.)

sort employee_ID 
merge employee_ID using  temp4.dta
keep if _merge==3
drop _merge
capture erase temp4.dta

for X in any newemployee formeremployee contemployee:gen wage0_X=earnings_lag if X==1

bys ID,sort: egen avg_wage0=mean(earnings0)

gen newemp_high=1 if wage0_newemployee>avg_wage0  & earnings0_newemployee!=.

collapse (mean) wage0_newemployee wage0_formeremployee wage0_contemployee avg_wage0 (sum) newemployee newemp_high,by(ID)

sort ID 
save temp3.dta,replace


//Lagged firm-level average wages

use temp2.dta,clear

keep if year==2011 | year==2012
keep if nace2<34
collapse (mean) avgwage_adj,by(ID)
rename avgwage_adj avgwage_adj_lag

sort ID
save temp4.dta,replace

//Identify new suppliers

use masterdata.dta,clear

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

gen period=0 if year==2011 | year==2012
replace period=1 if year==2014 |  year==2015
drop if period==.

collapse (sum) value (mean) avgwage_adj_s,by( buyer_ID seller_ID period )

reshape wide value  avgwage_adj_s,i(buyer_ID seller_ID) j(period)

gen newsupp=(value1!=. & value0==.)
gen formersupp=(value1==. & value0!=.)
gen contsupp=(value1!=. & value0!=.)

gen ID=seller_ID
sort ID
merge ID using temp4
drop if _merge==2
drop _merge ID

for X in any newsupp formersupp  contsupp: gen wage0_X=avgwage_adj_lag if X==1

bys buyer_ID,sort: egen avg_supwage0=mean(avgwage_adj_s0)

gen newsup_high=1 if wage0_newsupp>avg_supwage0  & avg_supwage0!=.

*Constructing the intensive margin term in eqn C.10
gen contavgwage_adj_lag=ln(avgwage_adj_lag) if contsupp==1
bys buyer_ID,sort: egen avg_contsupwage0=mean(contavgwage_adj_lag)
forvalues i=0(1)1 {
gen contvalue`i'=value`i' if contsupp==1
bys buyer_ID,sort: egen contvaluetotal`i'=sum(contvalue`i')
}
gen wage_int_contsup=((contvalue0/contvaluetotal0)-(contvalue1/contvaluetotal1))*(contavgwage_adj_lag-avg_contsupwage0)

collapse (mean) wage0_newsupp wage0_formersupp wage0_contsupp avg_supwage0  (sum) newsup_high newsupp wage_int_contsup,by(buyer_ID)

rename buyer_ID ID
destring ID,replace
sort ID
save temp5.dta,replace


//Identify new buyers

use masterdata.dta,clear

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

gen period=0 if year==2011 | year==2012
replace period=1 if year==2014 |  year==2015
drop if period==.

collapse (sum) value (mean) avgwage_adj_b,by( buyer_ID seller_ID period )

reshape wide value avgwage_adj_b ,i(buyer_ID seller_ID) j(period)

gen newbuyer=(value1!=. & value0==.)
gen formerbuyer=(value1==. & value0!=.)
gen contbuyer=(value1!=. & value0!=.)

gen ID=buyer_ID
sort ID
merge ID using temp4
drop if _merge==2
drop _merge ID
capture erase temp4.dta

for X in any newbuyer formerbuyer  contbuyer: gen wage0_X=avgwage_adj_lag if X==1

bys seller_ID,sort: egen avg_buyerwage0=mean(avgwage_adj_b0)

gen newbuyer_high=1 if wage0_newbuyer>avg_buyerwage0  & avg_buyerwage0!=.

*Constructing the intensive margin term in eqn C.10
gen contavgwage_adj_lag=ln(avgwage_adj_lag) if contbuyer==1
bys buyer_ID,sort: egen avg_contbuyerwage0=mean(contavgwage_adj_lag)
forvalues i=0(1)1 {
gen contvalue`i'=value`i' if contbuyer==1
bys seller_ID,sort: egen contvaluetotal`i'=sum(contvalue`i')
}
gen wage_int_contbuyer=((contvalue0/contvaluetotal0)-(contvalue1/contvaluetotal1))*(contavgwage_adj_lag-avg_contbuyerwage0)

collapse (mean) wage0_newbuyer wage0_formerbuyer wage0_contbuyer avg_buyerwage0 (sum) newbuyer_high newbuyer wage_int_contbuyer,by(seller_ID)

rename seller_ID ID
destring ID,replace
sort ID
save temp4.dta,replace

*Load master data
use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

forvalues i=3(1)5 {
	sort ID
	merge ID using temp`i'
	drop if _m==2
	drop _m
}

forvalues i=3(1)5 {
	capture erase temp`i'.dta
}

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

gen rel_worker_newtoall=ln(wage0_newemployee/avg_wage0 )
gen rel_sup_newtoall=ln(wage0_newsupp/avg_supwage0 )
gen rel_buyer_newtoall=ln(wage0_newbuyer / avg_buyerwage0 )

gen rel_worker_newtoformer=ln(wage0_newemployee/wage0_formeremployee )
gen rel_sup_newtoformer=ln(wage0_newsupp/wage0_formersupp )
gen rel_buyer_newtoformer=ln(wage0_newbuyer / wage0_formerbuyer )

gen shr_high_workers=newemp_high/newemployee 
gen shr_high_supp=newsup_high/ newsupp
gen shr_high_buyers=newbuyer_high/ newbuyer 

//Table 4

reghdfe  rel_worker_newtoall adjexshock ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  rel_sup_newtoall adjexshock ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  rel_buyer_newtoall adjexshock ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

// Table A14


reghdfe  shr_high_workers adjexshock ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  shr_high_supp adjexshock ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  shr_high_buyers adjexshock ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

//Table A15

reghdfe  rel_worker_newtoformer adjexshock ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  rel_sup_newtoformer adjexshock ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  rel_buyer_newtoformer adjexshock ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

//Table A16

reghdfe  wage_int_contsup adjexshock ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  wage_int_contbuyer adjexshock ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace


noi esttab col1 col2 , replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A10
****************************************/
*Note that some of the summary statistics are produced in "DataPrep"
use shiftsharesample.dta,clear

keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks 

su adjexshock,det
reghdfe adjexshock,absorb(nace4_s) residuals(res_adjexshock)
su res_adjexshock,det


/***************************************
/Table A11
****************************************/

use shiftsharesample.dta,clear

su adjexshock,det
keep if (adjexshock>r(p1) & adjexshock<r(p99)) | adjexshock==. // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

reghdfe  Chgavgwage_adj_s unadjexshock adjexshock,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s randomexshock,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s adjexshock expintensity0,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s adjexshock lngdppc_w_ex,absorb(sr) vce(cluster s)
estimates store col4
estadd local "FE" "sr" , replace

gen interaction=unadjexshock*lngdppc_w_ex
reghdfe  Chgavgwage_adj_s unadjexshock lngdppc_w_ex interaction,absorb(sr) vce(cluster s)
estimates store col5
estadd local "FE" "sr" , replace

for X in any adjexshock lngdppc_w_ex: replace X=0 if X==.
reghdfe  Chgavgwage_adj_s adjexshock lngdppc_w_ex,absorb(sr) vce(cluster s)
estimates store col6
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s gdpadjexshock ,absorb(sr) vce(cluster s)
estimates store col7
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3 col4 col5 col6 col7, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A12
****************************************/

use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

reghdfe  Chgw_fe  adjexshock,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  Chglnq  adjexshock,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

noi esttab col1 col2 , replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A13
****************************************/

use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

reghdfe  Chgworkers_s  adjexshock,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  Chgoutdegree  adjexshock,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  Chgindegree  adjexshock,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)



/***************************************
/Table A17
****************************************/
//First-degree connections
use masterdata.dta,clear

gen period=0 if year==2011 | year==2012
replace period=1 if year==2014 |  year==2015
keep if period==0

bys seller_ID period,sort: egen domsales=sum(value)

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

collapse (sum) value,by(buyer_ID seller_ID)

bys seller_ID,sort: egen total_value_s=sum(value)
gen value_share_s=value/total_value_s

bys buyer_ID ,sort: egen total_value_b=sum(value)
gen value_share_b=value/total_value_b

gen ID=seller_ID
destring ID,replace
sort ID
merge ID using  expshock.dta
drop if _m==2
drop _m unadjexshock gdpadjexshock lngdppc_w_ex randomexshock ID
rename adjexshock adjexshock_s
gen supshock_first=adjexshock_s*value_share_b

gen ID=buyer_ID
destring ID,replace
sort ID
merge ID using  expshock.dta
drop if _m==2
drop _m unadjexshock gdpadjexshock lngdppc_w_ex randomexshock ID
rename adjexshock adjexshock_b
gen buyershock_first=adjexshock_b*value_share_s

preserve

collapse (sum) supshock_first,by(buyer_ID)
rename buyer_ID ID
sort ID
save temp3.dta,replace

restore 

preserve
collapse (sum) buyershock_first,by(seller_ID)
rename seller_ID ID
sort ID
merge ID using temp3.dta
drop _m
sort ID
save temp3.dta,replace

restore

//Second-degree connections
use masterdata.dta,clear

gen period=0 if year==2011 | year==2012
replace period=1 if year==2014 |  year==2015
keep if period==0

bys seller_ID period,sort: egen domsales=sum(value)

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

collapse (sum) value,by(buyer_ID seller_ID)

bys seller_ID,sort: egen total_value_s=sum(value)
gen value_share_s=value/total_value_s

bys buyer_ID ,sort: egen total_value_b=sum(value)
gen value_share_b=value/total_value_b

gen ID=seller_ID
destring ID,replace
sort ID
merge ID using  temp3.dta
drop if _m==2
gen supshock_second=supshock_first*value_share_b
drop _m  ID buyershock_first supshock_first

gen ID=buyer_ID
destring ID,replace
sort ID
merge ID using  temp3.dta
drop if _m==2
gen buyershock_second=buyershock_first*value_share_s
drop _m  ID buyershock_first supshock_first

preserve

collapse (sum) supshock_second,by(buyer_ID)
rename buyer_ID ID
sort ID
merge ID using temp3.dta
drop _m
sort ID
save temp3.dta,replace

restore 

preserve
collapse (sum) buyershock_second,by(seller_ID)
rename seller_ID ID
sort ID
merge ID using temp3.dta
drop _m
destring ID,replace
sort ID
save temp3.dta,replace

*Load master data
use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

sort ID
merge ID using temp3.dta
drop if _m==2
drop _m
capture erase temp3.dta


reghdfe  Chgavgwage_adj_s adjexshock supshock_first supshock_second ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s adjexshock buyershock_first buyershock_second ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s adjexshock supshock_first supshock_second buyershock_first buyershock_second ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3 , replace compress width(1\hsize)  se  s(N r2 FE clustvar)



/***************************************
/Table A18
****************************************/

use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

replace adjimpshock=0 if adjimpshock==.

reghdfe  Chgavgwage_adj_s  adjimpshock,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s  adjimpshock adjexshock,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

noi esttab col1 col2 , replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A19
****************************************/

use shiftsharesample.dta,clear

su adjexshock,det
keep if adjexshock>r(p1) & adjexshock<r(p99) // Drop outliers in terms of export shocks

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s  )

replace adjimpshock=0 if adjimpshock==.

gen sector=""
for X in num 10/12: replace sector="10-12"	 if nace2==X
for X in num 13/18: replace sector="X"	 if nace2==X
for X in num 19/21: replace sector="19-21"	 if nace2==X
for X in num 22/25: replace sector="X"	 if nace2==X
for X in num 26/27: replace sector="26-27"	 if nace2==X
for X in num 28 29 31 32 33: replace sector="X"	 if nace2==X

gen High=(sector=="29" | sector=="17" | sector=="23" | sector=="22" | sector=="24" | sector=="26-27" | sector=="19-21" | sector=="25" | sector=="18") // Based on Figure A4

gen Highxadjexshock=High*adjexshock
gen Highxadjimpshock=High*adjimpshock

reghdfe  Chgavgwage_adj_s  adjimpshock Highxadjimpshock,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s  adjexshock Highxadjexshock,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  Chgavgwage_adj_s  adjimpshock Highxadjimpshock adjexshock Highxadjexshock,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

